<?php

class openapi_api_function_v1_oncredit extends openapi_api_function_abstract implements openapi_api_function_interface{

	/**
	 * 我的信用额度
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function myoncredit($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $npage = $params['npage']?$params['npage']:1;
        $pagesize = 10;
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $status = $params['credit_status'];
        $mdl_credit = app::get('business')->model('storemanger_oncredit');
        $inperiod_credit = $mdl_credit->getList('oncredit_id,period_start,credit_period',array('store_id'=>$storeinfo['store_id'],'status'=>'1'));
        $inperiod_credit_ids = array();
        foreach ($inperiod_credit as $k => $v) {
            if($v['period_start']+$v['credit_period']*86400>time()){
                $inperiod_credit_ids[] = $v['oncredit_id'];
            }
        }
        switch ($status) {
            case 'needprepaid':
                $filter['status'] = '2';
                break;
            case 'inperiod':
            case 'issued':
                $filter['status'] = '1';
                break;
            case 'waitreply':
                $filter['status'] = '3';
                break;
            case 'overdue':
                $filter['status|in'] = array('1','5');
                $filter['oncredit_id|notin'] = $inperiod_credit_ids;
                break;
            case 'cancel':
                $filter['status'] = '4';
                break;
            default:
                $status = 'apply';
                $filter['status'] = '0';
                break;
        }
        $filter['store_id'] = $storeinfo['store_id'];
        $data = $mdl_credit->getList('*', $filter,($npage-1)*$pagesize,$pagesize,"apply_time desc");

        if($status == 'inperiod'){
            foreach($data as $key=>$v){
                if(time()-$v['period_start'] > $v['credit_period']*86400){
                    unset($data[$key]);
                }
            }
        }

        if($status == 'overdue'){
            foreach($data as $key=>$v){
                $time = time()-$v['period_start'];
                $overdays = ceil($time/86400 - $v['credit_period']);
                $data[$key]['overdays'] = $overdays > 0 ? $overdays : 0;
                $data[$key]['interest'] = $mdl_credit->countInterest($v['oncredit_id']);
            }
        }

        if($status == 'finished'){
            foreach($data as $key=>$v){
                if(($time = $v['last_modify']-$v['period_start']) > $v['credit_period']*86400){
                    //$overdays = ceil($time/86400 - $v['credit_period']);
                    //$data[$key]['overdays'] = $overdays > 0 ? $overdays : 0;
                    $data[$key]['interest'] = $v['settled_interest'];
                }
            }
        }
        foreach ($data as $tk=>$tv) {
            $data[$tk]['apply_time'] = date('Y-m-d H:i:s',$tv['apply_time']);
            $data[$tk]['period_start'] = date('Y-m-d H:i:s',$tv['period_start']);
            if($status=='cancel'){
                $data[$tk]['cancel_time'] = date('Y-m-d H:i:s',$tv['last_modify']);
            }
        }
		return $this->apiData(true,$data,200,null);
	}

	/**
	 * 申请授信额度
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function oncreditApply($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
		$data['apply_money'] = isset($params['apply_money']) ? floatval($params['apply_money']) : 0;
        if($data['apply_money'] > 0){
            $oncredit = app::get('business')->model('storemanger_oncredit')->getList('*', array('store_id'=>$storeinfo['store_id'], 'status'=>'1'));
            foreach($oncredit as $v){
                if($v['period_start'] + $v['credit_period'] * 86400 > time()){
                    return $this->apiData(false,null,400,app::get('business')->_('申请失败：存在未过期的授信'));
                }
            }
            $oncredit0 = app::get('business')->model('storemanger_oncredit')->getList('*', array('store_id'=>$storeinfo['store_id'], 'status'=>'0'));
            if(!empty($oncredit0)){
                return $this->apiData(false,null,400,app::get('business')->_('申请失败：存在未审批的授信'));
            }

            $data['store_id'] = $storeinfo['store_id'];
            $data['apply_time'] = time();
            $data['reply_money'] = $data['apply_money'];
            $parent_id = $storeinfo['parent_id'];
            if(!$parent_id){
                return $this->apiData(false,null,400,app::get('business')->_('还没有上级经销商'));
            }
            $hq_store_id = kernel::single('b2c_goods_store')->hqStoreId();
            
            $data['parent_id'] = $parent_id;
            if(app::get('business')->model('storemanger_oncredit')->save($data)){
                $send_data['member_id'] = $memberInfo['member_id'];
                $send_data['uname'] = $memberInfo['uname'];
                $send_data['comment'] = app::get('b2c')->_($memberInfo['uname'].'提交了信用额度申请，申请额度：'.$data['apply_money']);
                if(in_array($parent_id, $hq_store_id)){
                    $send_data['to_type'] = 'admin';
                }else{
                    $supStore = app::get('business')->model('storemanger')->getRow('account_id',array('store_id'=>$parent_id));
                    $send_data['to_id'] = $supStore['account_id'];
                    $send_data['msg_to'] = app::get('pam')->model('account')->get_operactor_name($supStore['account_id']);
                }
                $send_data['subject'] = app::get('b2c')->_('信用额度申请');
                $send_data['has_sent'] = true;
                kernel::single('b2c_message_msg')->send($send_data);
                return $this->apiData(true,null,200,app::get('b2c')->_('提交成功，请耐心等待审核'));
            }else{
                return $this->apiData(false,null,400,app::get('business')->_('提交失败'));
            }
        }else{
            return $this->apiData(false,null,400,app::get('business')->_('金额有误'));
        }
	}

	/**
	 * 下放授信额度
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function toIssue($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $substore = app::get("business")->model('storemanger')->getList('store_id,store_name',array('parent_id'=>$storeinfo['store_id']));
        return $this->apiData(true,$substore,200,null);
	}

	/**
	 * 下放额度
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function issueSave($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        if(!$params['store_id']){
            return $this->apiData(false,null,400,app::get('business')->_('请选择一个经销商'));
        }
        $mdl_oncredit = app::get('business')->model('storemanger_oncredit');
        $oncredit = $mdl_oncredit->getList('*', array('store_id'=>$params['store_id'], 'status'=>'1'));
        foreach($oncredit as $v){
            if($v['period_start'] + $v['credit_period'] * 86400 > time()){
                return $this->apiData(false,null,400,app::get('business')->_('下放失败：存在未过期的授信'));
            }
        }

        if(!$params['reply_money'] || intval($params['reply_money']) <= 0){
            return $this->apiData(false,null,400,app::get('business')->_('额度设置不正确'));
        }
        if(!$params['credit_period'] || intval($params['credit_period']) <= 0){
            return $this->apiData(false,null,400,app::get('business')->_('账期设置不正确'));
        }

        $myoncredit = $mdl_oncredit->getList('*', array('store_id'=>$storeinfo['store_id'], 'status'=>'1'));
        foreach($myoncredit as $v){
            if(($period_end = $v['period_start'] + $v['credit_period'] * 86400) > time()){
                if(floor(($period_end - time()) / 86400) < $params['credit_period']){
                    return $this->apiData(false,null,400,app::get('business')->_('审核失败：账期超出可设范围'));
                }
                $data1 = $v;
            }
        }

        if($data1['usable_money']< floatval($params['reply_money'])){
            $data1['usable_money'] = $data1['usable_money']?$data1['usable_money']:'0';
            return $this->apiData(false,null,400,app::get('business')->_('额度不够，只剩'.$data1['usable_money'].'的额度'));
        }else{
            $db = kernel::database();
            $transaction_status = $db->beginTransaction();

            $data['store_id'] = $params['store_id'];
            $storemanger = app::get('business')->model('storemanger')->dump($data['store_id'], 'parent_id,account_id');
            $data['parent_id'] = $storemanger['parent_id'];
            $data['apply_money'] = intval($params['reply_money']);
            $data['reply_money'] = $data['apply_money'];
            $data['usable_money'] = $data['reply_money'];
            $data['apply_time'] = time();
            $data['credit_period'] = intval($params['credit_period']);
            $data['period_start'] = time();
            $data['status'] = '1';
            $data['memo'] = $params['memo'];
            $data['last_modify'] = time();
            $data['flag'] = '1';
            if(!empty($params['interest_rates']))$data['interest_rates']=$params['interest_rates'];
            if($mdl_oncredit->save($data)){
                $data1['usable_money'] -= $params['reply_money'];
                if($mdl_oncredit->save($data1)){
                    //记录额度流水 我的额度变化
                    $mylog = array();
                    $mylog['oncredit_id'] = $data1['oncredit_id'];
                    $mylog['type'] = 'money';
                    $mylog['value'] = 0 - $data['usable_money'];
                    $memberInfo = $this->get_member_info();
                    $mylog['op_id'] = $memberInfo['member_id'];
                    $mylog['op_name'] = $memberInfo['uname'];
                    $mylog['oncredit_amount'] = $data1['usable_money'];
                    $mylog['memo'] = app::get('business')->_('给下级下发');
                    app::get('business')->model('storemanger_oncredit_log')->save($mylog);
                    //下级额度变化
                    $log = array();
                    $log['oncredit_id'] = $data['oncredit_id'];
                    $log['type'] = 'money';
                    $log['value'] = $data['usable_money'];
                    $log['op_id'] = $memberInfo['member_id'];
                    $log['op_name'] = $memberInfo['uname'];
                    $log['oncredit_amount'] = $data['usable_money'];
                    $log['memo'] = app::get('business')->_('上级下发');
                    app::get('business')->model('storemanger_oncredit_log')->save($log);
                    //发送站内信
                    $send_data['member_id'] = $memberInfo['member_id'];
                    $send_data['uname'] = $memberInfo['uname'];
                    $send_data['comment'] = app::get('b2c')->_('上级给你派发了信用额度，下发额度：'.$params['reply_money'].',额度期限：'.$params['credit_period'].'天');
                    $subStore = app::get('business')->model('storemanger')->getRow('account_id',array('store_id'=>$params['store_id']));
                    $send_data['to_id'] = $subStore['account_id'];
                    $send_data['msg_to'] = app::get('pam')->model('account')->get_operactor_name($subStore['account_id']);
                    $send_data['subject'] = app::get('b2c')->_('信用额度通知');
                    $send_data['has_sent'] = true;
                    kernel::single('b2c_message_msg')->send($send_data);
                    $db->commit();
                    return $this->apiData(true,null,200,app::get('b2c')->_('提交成功'));
                }else{
                    $db->rollback();
                    return $this->apiData(false,null,400,app::get('business')->_('操作失败'));
                }
            }else{
                $db->rollback();
                return $this->apiData(false,null,400,app::get('business')->_('操作失败'));
            }
        }
	}

    /**
     * 取消申请
     * @param
     * @return
     * @author hmg
     **/
    public function cancelOncredit($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));

        $oncredit_id = $params['oncredit_id'];
        $data = app::get('business')->model('storemanger_oncredit')->dump($oncredit_id);

        if($data['status'] > 0){
            return $this->apiData(false,null,400,app::get('business')->_('非法操作'));
        }else{
            $data['last_modify'] = time();
            $data['status'] = '4';
            $data['memo'] = app::get('business')->_('自己取消');
            if(app::get('business')->model('storemanger_oncredit')->save($data)){
                return $this->apiData(true,null,200,app::get('b2c')->_('取消成功'));
            }else{
                return $this->apiData(false,null,400,app::get('b2c')->_('取消失败'));
            }
        }
    }

    /**
     * 下级额度
     * @param
     * @return
     * @author hmg
     **/
    public function sub_oncredit($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $npage = $params['npage']?$params['npage']:1;
        $pagesize = 10;
        $omath = kernel::single('ectools_math');
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $store_id = $params['substore_id'];
        $status = $params['credit_status'];
        if($store_id){
            $mdl_credit = app::get('business')->model('storemanger_oncredit');
            $inperiod_credit = $mdl_credit->getList('oncredit_id,period_start,credit_period',array('store_id'=>$store_id,'status'=>'1'));
            $inperiod_credit_ids = array();
            foreach ($inperiod_credit as $k => $v) {
                if($v['period_start']+$v['credit_period']*86400>time()){
                    $inperiod_credit_ids[] = $v['oncredit_id'];
                }
            }
            switch ($status) {
                case 'needprepaid':
                    $filter['status'] = '2';
                    break;
                case 'inperiod':
                case 'issued':
                    $filter['status'] = '1';
                    break;
                case 'waitreply':
                    $filter['status'] = '3';
                    break;
                case 'overdue':
                    $filter['status|in'] = array('1','5');
                    $filter['oncredit_id|notin'] = $inperiod_credit_ids;
                    break;
                case 'cancel':
                    $filter['status'] = '4';
                    break;
                default:
                    $status = 'apply';
                    $filter['status'] = '0';
                    break;
            }
            $filter['store_id'] = $store_id;
            $data = $mdl_credit->getList('*', $filter,($npage-1)*$pagesize,$pagesize,"apply_time desc");
            $count = $mdl_credit->count($filter);
            if($status == 'inperiod'){
                foreach($data as $key=>$v){
                    if(time()-$v['period_start'] > $v['credit_period']*86400){
                        unset($data[$key]);
                    }
                }
            }

            if($status == 'overdue'){
                foreach($data as $key=>$v){
                    $time = time()-$v['period_start'];
                    $overdays = ceil($time/86400 - $v['credit_period']);
                    $data[$key]['overdays'] = $overdays > 0 ? $overdays : 0;
                    $data[$key]['interest'] = $mdl_credit->countInterest($v['oncredit_id']);
                }
            }

            if($status == 'finished'){
                foreach($data as $key=>$v){
                    if(($time = $v['last_modify']-$v['period_start']) > $v['credit_period']*86400){
                        //$overdays = ceil($time/86400 - $v['credit_period']);
                        //$data[$key]['overdays'] = $overdays > 0 ? $overdays : 0;
                        $data[$key]['interest'] = $v['settled_interest'];
                    }
                }
            }

            foreach ($data as &$v) {
                $v['apply_time'] = date('Y-m-d H:i:s',$v['apply_time']);
                $v['period_start'] = date('Y-m-d H:i:s',$v['period_start']);
                if($status=='cancel'){
                    $v['cancel_time'] = date('Y-m-d H:i:s',$v['last_modify']);
                }
            }

            $this->pagedata['data'] = $data;
            $this->pagedata['status'] = $status;
            $this->pagedata['store_id'] = $store_id;
        }else{
            $data1 = app::get('business')->model('storemanger_oncredit')->getList('store_id', array('parent_id'=>$storeinfo['store_id']));
            foreach($data1 as $v){
                $sub_store[$v['store_id']]['store_id'] = $v['store_id'];
                $sub_store[$v['store_id']]['num'] = 0;
                $sub_store[$v['store_id']]['amount'] = 0;
            }

            $sql = "select store_id,count(*) as num,sum(reply_money) as amount from sdb_business_storemanger_oncredit where (status = '1' or status = '5') and parent_id =".$storeinfo['store_id']." group by store_id";
            $data2 = app::get('business')->model('storemanger_oncredit')->db->select($sql);
            foreach($data2 as $v){
                $sub_store[$v['store_id']]['num'] = $v['num'];
                $sub_store[$v['store_id']]['amount'] = $omath->get($v['amount'],2);
            }
            $sub_store_id = array_keys($sub_store);
            $sub_store_name = app::get('business')->model('storemanger')->getList('store_id,store_name', array('store_id|in'=>$sub_store_id));
            foreach($sub_store_name as $v){
                $sub_store[$v['store_id']]['store_name'] = $v['store_name'];
            }
            $this->pagedata['store_id'] = null;
            $this->pagedata['data'] = $sub_store;
        }
        $this->pagedata['data'] = kernel::single('openapi_utils_datahelper')->array_merge_fb($this->pagedata['data']);
        return $this->apiData(true,$this->pagedata,200);
    }

    /**
     * 我的额度使用明细
     * @param
     * @return
     * @author hmg
     **/
    public function oncredit_detail($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $omath = kernel::single('ectools_math');
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $credit_id = $params['oncredit_id'];
        $oncredit = app::get('business')->model('storemanger_oncredit')->getRow('*',array('oncredit_id'=>$credit_id));
        if($oncredit['store_id']!=$storeinfo['store_id']){
            return $this->apiData(false,null,400,app::get('b2c')->_('无权查看'));
        }
        $page = $params['page']?$params['page']:1;
        $pagesize = 10;
        $mdl_credit_log = app::get('business')->model('storemanger_oncredit_log');
        $credit_logs = $mdl_credit_log->getList('*',array('oncredit_id'=>$credit_id),($page-1)*$pagesize,$pagesize,'createtime DESC');
        foreach ($credit_logs as &$v) {
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            if($v['type']=='period'){
                $v['oncredit_period'] = $v['value'];
                $v['value'] = 0;
            }
            if(!empty($v['bill_id'])){
                $v['memo'] .= $v['bill_id'];
            }
        }
        return $this->apiData(true,$credit_logs,200);
    }

    /**
     * 审核下级申请
     * @param
     * @return
     * @author hmg
     **/
    public function suboncredit_opt($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $omath = kernel::single('ectools_math');
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $o = app::get('business')->model('storemanger_oncredit');
        $data = $o->dump($params['oncredit_id']);
        if($data['parent_id']!=$storeinfo['store_id']){
            return $this->apiData(false,null,400,app::get('b2c')->_('无操作权限'));
        }
        //上级审批通过
        if($params['agree']){
            if(empty($params['credit_period']) || empty($params['reply_money'])){
                return $this->apiData(false,null,400,app::get('b2c')->_('账期和金额必填'));
            }
            $sub_oncredit = $o->getList('*', array('store_id'=>$data['store_id'], 'status'=>'1'));
            foreach($sub_oncredit as $v){
                if($v['period_start'] + $v['credit_period'] * 86400 > time()){
                    return $this->apiData(false,null,400,app::get('business')->_('审核失败：存在未过期的授信'));
                }
            }

            $oncredit = $o->getList('*', array('store_id'=>$storeinfo['store_id'], 'status'=>'1'));
            foreach($oncredit as $v){
                if(($period_end = $v['period_start'] + $v['credit_period'] * 86400) > time()){
                    if(floor(($period_end - time()) / 86400) < $params['credit_period']){
                        return $this->apiData(false,null,400,app::get('business')->_('审核失败：账期超出可设范围'));
                    }
                    $data1 = $v;
                }
            }

            $data['reply_money'] = floatval($params['reply_money']);
            $data['usable_money'] = $data['reply_money'];
            if($data1['usable_money']< $data['reply_money']){
                $data1['usable_money'] = $data1['usable_money']?$data1['usable_money']:'0.00';
                return $this->apiData(false,null,400,app::get('business')->_('额度不够，只剩'.$data1['usable_money'].'的额度'));
            }else{
                $db = kernel::database();
                $transaction_status = $db->beginTransaction();
                $data['last_modify'] = time();
                $data['status'] = '1';
                $data['credit_period'] = intval($params['credit_period']);
                $data['period_start'] = time();
                if(!empty($params['interest_rates'])) $data['interest_rates']=$params['interest_rates'];
                if($o->save($data)){
                    $data1['usable_money'] -= $params['reply_money'];
                    if($o->save($data1)){
                        //记录额度流水  我的额度变化
                        $mylog = array();
                        $mylog['oncredit_id'] = $data1['oncredit_id'];
                        $mylog['type'] = 'money';
                        $mylog['value'] = 0 - $data['usable_money'];
                        $memberInfo = $this->get_member_info();
                        $mylog['op_id'] = $memberInfo['member_id'];
                        $mylog['op_name'] = $memberInfo['uname'];
                        $mylog['oncredit_amount'] = $data1['usable_money'];
                        $mylog['memo'] = app::get('business')->_('给下级审批');
                        app::get('business')->model('storemanger_oncredit_log')->save($mylog);
                        //下级额度变化
                        $log = array();
                        $log['oncredit_id'] = $data['oncredit_id'];
                        $log['type'] = 'money';
                        $log['value'] = $data['usable_money'];
                        $memberInfo = $this->get_member_info();
                        $log['op_id'] = $memberInfo['member_id'];
                        $log['op_name'] = $memberInfo['uname'];
                        $log['oncredit_amount'] = $data['usable_money'];
                        $log['memo'] = app::get('business')->_('上级审批');
                        app::get('business')->model('storemanger_oncredit_log')->save($log);
                        //发送站内信
                        $memberInfo = $this->get_member_info();
                        $send_data['member_id'] = $memberInfo['member_id'];
                        $send_data['uname'] = $memberInfo['uname'];
                        $send_data['comment'] = app::get('b2c')->_('上级通过了你的额度申请，批复额度：'.$data['reply_money'].',额度期限：'.$data['credit_period'].'天');
                        $subStore = app::get('business')->model('storemanger')->getRow('account_id',array('store_id'=>$data['store_id']));
                        $send_data['to_id'] = $subStore['account_id'];
                        $send_data['msg_to'] = app::get('pam')->model('account')->get_operactor_name($subStore['account_id']);
                        $send_data['subject'] = app::get('b2c')->_('额度审批通知');
                        $send_data['has_sent'] = true;
                        kernel::single('b2c_message_msg')->send($send_data);
                        $db->commit($transaction_status);
                        return $this->apiData(true,null,200,app::get('business')->_('设置成功'));
                    }else{
                        $db->rollback();
                        return $this->apiData(false,null,400,app::get('business')->_('设置失败'));
                    }
                }else{
                    $db->rollback();
                    return $this->apiData(false,null,400,app::get('business')->_('设置失败'));
                }
            }
        }else{//拒绝申请
            if($data['status'] > 0){
                return $this->apiData(false,null,400,app::get('business')->_('非法操作'));
            }else{
                $data['memo'] = $params['memo'];
                $data['last_modify'] = time();
                $data['status'] = '4';
                if($o->save($data)){
                    return $this->apiData(true,null,200,app::get('business')->_('设置成功'));
                }else{
                    return $this->apiData(false,null,400,app::get('business')->_('设置失败'));
                }
            }
        }
    }

    /**
     * 调整账期和额度
     * @param
     * @return
     * @author hmg
     **/
    public function editsubcredit($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $o = app::get('business')->model('storemanger_oncredit');
        $data = $o->dump($params['oncredit_id']);
        if(!empty($params['credit_period']) && $data['credit_period'] > $params['credit_period']){
            return $this->apiData(false,null,400,app::get('business')->_('账期不能比原来小,原账期：'.$data['credit_period']));
        }
        if(!empty($params['reply_money']) && $data['reply_money'] > $params['reply_money']){
            return $this->apiData(false,null,400,app::get('business')->_('额度不能比原来小,原额度：'.$data['reply_money']));
        }
        
        $oncredit = $o->getList('*', array('store_id'=>$storeinfo['store_id'], 'status'=>'1'));
        foreach($oncredit as $v){
            if(($period_end = $v['period_start'] + $v['credit_period'] * 86400) > time()){
                if(!empty($params['credit_period']) && floor(($period_end - time()) / 86400) < $params['credit_period']){
                    return $this->apiData(false,null,400,app::get('business')->_('更新错误：账期超出可设范围'));
                }
                if(!empty($params['reply_money']) && $v['usable_money']<$params['reply_money']){
                    return $this->apiData(false,null,400,app::get('business')->_('超过可用额度：'.$v['usable_money']));
                }
            }
        }

        $data['oncredit_id'] = $params['oncredit_id'];
        if(!empty($params['reply_money']) && $params['reply_money']!=$data['reply_money']){
            $log['value'] = $params['reply_money'] - $data['reply_money'];
            $log['oncredit_amount'] = $params['reply_money'];
            $data['usable_money'] += $log['value'];
            $data['reply_money'] = $params['reply_money'];
            $editmoney = true;
        }
        if(!empty($params['credit_period']) && $params['credit_period']!=$data['credit_period']){
            $data['credit_period'] = $params['credit_period'];
            $log1['value'] = $params['credit_period'];
            $editperiod = true;
        }
        if(trim($params['memo'])) $data['memo'] = trim($params['memo']);
        $data['last_modify'] = time();
        if($o->save($data)){
            if($editmoney){
                $log['oncredit_id'] = $data['oncredit_id'];
                $log['type'] = 'money';
                $memberInfo = $this->get_member_info();
                $log['op_id'] = $memberInfo['member_id'];
                $log['op_name'] = $memberInfo['uname'];
                $log['memo'] = app::get('business')->_('额度调整');
                app::get('business')->model('storemanger_oncredit_log')->save($log);
                //发送站内信
                $send_data['member_id'] = $memberInfo['member_id'];
                $send_data['uname'] = $memberInfo['uname'];
                $send_data['comment'] = app::get('b2c')->_('额度调整通知，额度调整为：'.$data['reply_money']);
                $subStore = app::get('business')->model('storemanger')->getRow('account_id',array('store_id'=>$data['store_id']));
                $send_data['to_id'] = $subStore['account_id'];
                $send_data['msg_to'] = app::get('pam')->model('account')->get_operactor_name($subStore['account_id']);
                $send_data['subject'] = app::get('b2c')->_('额度调整通知');
                $send_data['has_sent'] = true;
                kernel::single('b2c_message_msg')->send($send_data);
            }
            if($editperiod){
                $log1['oncredit_id'] = $data['oncredit_id'];
                $log1['oncredit_amount'] = $data['reply_money'];
                $log1['type'] = 'period';
                $memberInfo = $this->get_member_info();
                $log1['op_id'] = $memberInfo['member_id'];
                $log1['op_name'] = $memberInfo['uname'];
                $log1['memo'] = app::get('business')->_('账期调整');
                app::get('business')->model('storemanger_oncredit_log')->save($log1);
                //发送站内信
                $send_data1['member_id'] = $memberInfo['member_id'];
                $send_data1['uname'] = $memberInfo['uname'];
                $send_data1['comment'] = app::get('b2c')->_('账期调整通知，账期调整为：'.$data['credit_period'].'天');
                $subStore = app::get('business')->model('storemanger')->getRow('account_id',array('store_id'=>$data['store_id']));
                $send_data1['to_id'] = $subStore['account_id'];
                $send_data1['msg_to'] = app::get('pam')->model('account')->get_operactor_name($subStore['account_id']);
                $send_data1['subject'] = app::get('b2c')->_('账期调整通知');
                $send_data1['has_sent'] = true;
                kernel::single('b2c_message_msg')->send($send_data1);
            }
            return $this->apiData(true,null,200,app::get('business')->_('设置成功'));
        }else{
            return $this->apiData(false,null,400,app::get('business')->_('设置失败'));
        }
    }

    /**
     * 调整利率
     * @param
     * @return
     * @author hmg
     **/
    public function editRates($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(!$member_id){
            return $this->apiData(false,null,301,app::get()->_('请登录'));
        }
        $storeinfo = app::get('business')->model('storemanger')->getRow('*',array('account_id'=>$member_id));
        $data = app::get('business')->model('storemanger_oncredit')->dump($params['oncredit_id']);
        if($data['parent_id'] != $storeinfo['store_id']){
            return $this->apiData(false,null,400,app::get('b2c')->_('无权修改'));
        }
        $data['oncredit_id'] = $params['oncredit_id'];
        $data['interest_rates'] = $params['interest_rates'];
        $data['last_modify'] = time();
        $o = app::get('business')->model('storemanger_oncredit');
        if($o->save($data)){
            return $this->apiData(true,null,200,app::get('b2c')->_('设置成功'));
        }else{
            return $this->apiData(false,null,400,app::get('b2c')->_('设置失败'));
        }
    }

}